﻿package  {
	import com.guavestudios.fcfg.Fcfg;
	import com.guavestudios.fcfg.FcfgEvent;
	import com.guavestudios.fcfg.readers.FcfgDefaultReader;
	import com.guavestudios.fcfg.readers.FcfgXmlReader;
	import flash.display.DisplayObject;
	import flash.display.Sprite;
	import flash.text.TextField;
	/**
	 * ...
	 * @author maze | guavestudios.com
	 */
	public class FcfgMain extends Sprite{
		
		
		public function FcfgMain() {
			
			var cfg:Fcfg = Fcfg.getUniqueInstance();
			
			var def:FcfgDefaultReader = new FcfgDefaultReader();
			cfg.addReader(def);
			def.idMap = { "main.config1":"config1"};
			def.load( { config:true, config1:false },stage.loaderInfo.parameters );
			
			var xml:FcfgXmlReader = new FcfgXmlReader(//);
			//xml.load(
				<root>
					<setting id="config2">lalalal</setting>
					<setting id="config3" type="boolean">true</setting>
					<setting id="config4" type="int">123.7</setting>
				</root>
			);
			cfg.addReader(xml);
			
			
			// simple unittest
			
			assertTrue("default config", cfg.getSetting("config"), true);
			
			cfg.setSetting("config", false);
			
			assertTrue("config modified", cfg.getSetting("config"), false);
			
			cfg.removeSetting("config");
			
			assertTrue("config removed", cfg.getSetting("config"), true);
			
			
			cfg.addReader( { config:false } );
			
			assertTrue("added reader", cfg.getSetting("config"), false);
			
		}
		
		public function sample2():void {
			var cfg:Fcfg = Fcfg.getUniqueInstance();
			//initialize application default values
			cfg.addReader( { 
				config:"default value"
			});
			
			//add stage parameters as values FlashVars: config:"flashvar value"
			cfg.addReader( stage.loaderInfo.parameters );
			
			trace("config: " + cfg.g("config")); //long version: cfg.getSetting
			//Output: config: flashvar value
			
			//....
			//Later on an xml get loaded
			var xml:XML = new XML("<root><setting id='config'>xml value</setting><root>");
			cfg.addReader(xml);
			
			trace("config: " + cfg.g("config"));
			//Output: config: xml value
			
		}
		
		public function sample3():void {
			var cfg:Fcfg = Fcfg.getUniqueInstance();
			//initialize application default values
			cfg.addReader( { 
				config:"default value"
			});
			
			cfg.s("config", "overriden value"); //long version: cfg.setSetting
			trace("config: " + cfg.g("config"));
			//Output: config: overriden value
			
			//now lets remove the setting
			cfg.r("config"); //long version: cfg.removeSetting
			trace("config: " + cfg.g("config"));
			//Output: config: default value
		}
		
		public function sample4():void {
			var cfg:Fcfg = Fcfg.getUniqueInstance();
			cfg.addEventListener(FcfgEvent.SETTING_CHANGED, cfg_changed);
			
			cfg.setSetting("config", "modified setting");
		}
		
		private function cfg_changed(e:FcfgEvent):void {
			trace("changed: ", e.id, e.newValue, e.oldValue);
			//Output changed: config,modified setting,null
		}
		public function sample5():void {
			var cfg:Fcfg = Fcfg.getUniqueInstance(); //unique generated name
			cfg.configName; //stored name, can be recalled with this
			
			var namedCfg:Fcfg=Fcfg.getInstance("myConfig");
			var otherContextCfg:Fcfg = Fcfg.getInstance("myConfig");
			//otherContextCfg and namedCfg reference to the same object
		}
		public function assertTrue(message:String,value:*,result:*):void {
			if (value == result) {
				trace("(FcfgMain.PASS) "+message);
			} else {
				trace("(FcfgMain.FAIL) "+message+" - "+value+"=="+result);
			}
		}
	}

}